/*     */ package com.zimbra.cs.util;
/*     */ 
/*     */ import com.zimbra.common.auth.ZAuthToken;
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.AdminConstants;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.soap.Element.Disposition;
/*     */ import com.zimbra.common.soap.Element.XMLElement;
/*     */ import com.zimbra.common.soap.SoapFaultException;
/*     */ import com.zimbra.common.soap.SoapHttpTransport;
/*     */ import com.zimbra.common.soap.SoapTransport;
/*     */ import com.zimbra.common.util.StringUtil;
/*     */ import com.zimbra.cs.account.Config;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.client.LmcSession;
/*     */ import java.io.IOException;
/*     */ import java.io.PrintStream;
/*     */ import java.io.PrintWriter;
/*     */ import java.net.URL;
/*     */ import java.net.UnknownHostException;
/*     */ import java.text.ParsePosition;
/*     */ import java.text.SimpleDateFormat;
/*     */ import java.util.Date;
/*     */ import java.util.HashSet;
/*     */ import java.util.Set;
/*     */ import org.apache.commons.cli.CommandLine;
/*     */ import org.apache.commons.cli.CommandLineParser;
/*     */ import org.apache.commons.cli.GnuParser;
/*     */ import org.apache.commons.cli.HelpFormatter;
/*     */ import org.apache.commons.cli.Option;
/*     */ import org.apache.commons.cli.OptionGroup;
/*     */ import org.apache.commons.cli.Options;
/*     */ import org.apache.commons.cli.ParseException;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public abstract class SoapCLI
/*     */ {
/*     */   public static final String O_AUTHTOKEN = "y";
/*     */   public static final String O_AUTHTOKENFILE = "Y";
/*     */   public static final String O_H = "h";
/*     */   public static final String O_HIDDEN = "hidden";
/*     */   public static final String O_S = "s";
/*  89 */   public static final Option OPT_AUTHTOKEN = new Option("y", "authtoken", true, "use auth token string (has to be in JSON format) from command line");
/*  90 */   public static final Option OPT_AUTHTOKENFILE = new Option("Y", "authtokenfile", true, "read auth token (has to be in JSON format) from a file");
/*     */   
/*     */   private String mUser;
/*     */   
/*     */   private String mPassword;
/*     */   
/*     */   private String mHost;
/*     */   private int mPort;
/*     */   private boolean mAuth;
/*     */   private Options mOptions;
/*     */   private Options mHiddenOptions;
/*     */   private boolean mDisableTargetServerOption;
/* 102 */   private SoapTransport mTrans = null;
/*     */   private String mServerUrl;
/*     */   
/*     */   protected SoapCLI() throws ServiceException {
/* 106 */     this(false);
/*     */   }
/*     */   
/*     */   protected SoapCLI(boolean disableTargetServerOption) throws ServiceException
/*     */   {
/* 111 */     this.mUser = LC.zimbra_ldap_user.value();
/*     */     
/* 113 */     this.mPassword = LC.zimbra_ldap_password.value();
/*     */     
/* 115 */     this.mHost = "localhost";
/*     */     
/* 117 */     Config conf = null;
/*     */     try {
/* 119 */       conf = Provisioning.getInstance().getConfig();
/*     */     } catch (ServiceException e) {
/* 121 */       throw ServiceException.FAILURE("Unable to connect to LDAP directory", e);
/*     */     }
/* 123 */     this.mPort = conf.getIntAttr("zimbraAdminPort", 0);
/* 124 */     if (this.mPort == 0)
/* 125 */       throw ServiceException.FAILURE("Unable to get admin port number from provisioning", null);
/* 126 */     this.mOptions = new Options();
/* 127 */     this.mHiddenOptions = new Options();
/* 128 */     this.mDisableTargetServerOption = disableTargetServerOption;
/*     */   }
/*     */   
/*     */   protected void setServer(String hostname) {
/* 132 */     this.mHost = hostname;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   protected CommandLine getCommandLine(String[] args)
/*     */     throws ParseException
/*     */   {
/* 142 */     CommandLineParser clParser = new GnuParser();
/* 143 */     CommandLine cl = null;
/*     */     
/* 145 */     Options opts = getAllOptions();
/*     */     try {
/* 147 */       cl = clParser.parse(opts, args);
/*     */     } catch (ParseException e) {
/* 149 */       if (helpOptionSpecified(args)) {
/* 150 */         usage();
/* 151 */         return null;
/*     */       }
/* 153 */       throw e;
/*     */     }
/* 155 */     if (cl.hasOption("h")) {
/* 156 */       boolean showHiddenOptions = cl.hasOption("hidden");
/* 157 */       usage(null, showHiddenOptions);
/* 158 */       return null;
/*     */     }
/* 160 */     if ((!this.mDisableTargetServerOption) && (cl.hasOption("s")))
/* 161 */       setServer(cl.getOptionValue("s"));
/* 162 */     return cl;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private Options getAllOptions()
/*     */   {
/* 171 */     Options newOptions = new Options();
/* 172 */     Set<OptionGroup> groups = new HashSet();
/* 173 */     Options[] optionses = { this.mOptions, this.mHiddenOptions };
/*     */     Options options;
/* 175 */     for (options : optionses) {
/* 176 */       for (Option opt : options.getOptions()) {
/* 177 */         OptionGroup group = options.getOptionGroup(opt);
/* 178 */         if (group != null) {
/* 179 */           groups.add(group);
/*     */         } else {
/* 181 */           newOptions.addOption(opt);
/*     */         }
/*     */       }
/*     */     }
/*     */     
/* 186 */     for (OptionGroup group : groups) {
/* 187 */       newOptions.addOptionGroup(group);
/*     */     }
/* 189 */     return newOptions;
/*     */   }
/*     */   
/*     */   private boolean helpOptionSpecified(String[] args) {
/* 193 */     return (args != null) && (args.length == 1) && (("-h".equals(args[0])) || ("--help".equals(args[0])));
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   protected LmcSession auth()
/*     */     throws SoapFaultException, IOException, ServiceException
/*     */   {
/* 205 */     URL url = new URL("https", this.mHost, this.mPort, "/service/admin/soap/");
/* 206 */     this.mServerUrl = url.toExternalForm();
/* 207 */     SoapTransport trans = getTransport();
/* 208 */     this.mAuth = false;
/*     */     
/* 210 */     Element authReq = new Element.XMLElement(AdminConstants.AUTH_REQUEST);
/* 211 */     authReq.addAttribute("name", this.mUser, Element.Disposition.CONTENT);
/* 212 */     authReq.addAttribute("password", this.mPassword, Element.Disposition.CONTENT);
/*     */     try {
/* 214 */       Element authResp = trans.invokeWithoutSession(authReq);
/* 215 */       String authToken = authResp.getAttribute("authToken");
/* 216 */       ZAuthToken zat = new ZAuthToken(null, authToken, null);
/* 217 */       trans.setAuthToken(authToken);
/* 218 */       this.mAuth = true;
/* 219 */       return new LmcSession(zat, null);
/*     */     }
/*     */     catch (UnknownHostException e) {
/* 222 */       throw new IOException("Unknown host: " + this.mHost);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   protected LmcSession auth(ZAuthToken zAuthToken)
/*     */     throws SoapFaultException, IOException, ServiceException
/*     */   {
/* 233 */     if (zAuthToken == null) {
/* 234 */       return auth();
/*     */     }
/* 236 */     URL url = new URL("https", this.mHost, this.mPort, "/service/admin/soap/");
/* 237 */     this.mServerUrl = url.toExternalForm();
/* 238 */     SoapTransport trans = getTransport();
/* 239 */     this.mAuth = false;
/*     */     
/* 241 */     Element authReq = new Element.XMLElement(AdminConstants.AUTH_REQUEST);
/* 242 */     zAuthToken.encodeAuthReq(authReq, true);
/*     */     try {
/* 244 */       Element authResp = trans.invokeWithoutSession(authReq);
/* 245 */       ZAuthToken zat = new ZAuthToken(authResp.getElement("authToken"), true);
/* 246 */       trans.setAuthToken(zat);
/* 247 */       this.mAuth = true;
/* 248 */       return new LmcSession(zat, null);
/*     */     }
/*     */     catch (UnknownHostException e) {
/* 251 */       throw new IOException("Unknown host: " + this.mHost);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   protected void setupCommandLineOptions()
/*     */   {
/* 260 */     if (!this.mDisableTargetServerOption) {
/* 261 */       Option s = new Option("s", "server", true, "Mail server hostname. Default is localhost.");
/* 262 */       this.mOptions.addOption(s);
/*     */     }
/* 264 */     this.mOptions.addOption("h", "help", false, "Displays this help message.");
/* 265 */     this.mHiddenOptions.addOption(null, "hidden", false, "Include hidden options in help output");
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   protected void usage()
/*     */   {
/* 273 */     usage(null);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   protected void usage(ParseException e)
/*     */   {
/* 281 */     usage(e, false);
/*     */   }
/*     */   
/*     */   protected void usage(ParseException e, boolean showHiddenOptions) {
/* 285 */     if (e != null) {
/* 286 */       System.err.println("Error parsing command line arguments: " + e.getMessage());
/*     */     }
/*     */     
/* 289 */     Options opts = showHiddenOptions ? getAllOptions() : this.mOptions;
/* 290 */     PrintWriter pw = new PrintWriter(System.err, true);
/* 291 */     HelpFormatter formatter = new HelpFormatter();
/* 292 */     formatter.printHelp(pw, formatter.getWidth(), getCommandUsage(), null, opts, formatter.getLeftPadding(), formatter.getDescPadding(), null);
/*     */     
/*     */ 
/* 295 */     pw.flush();
/*     */     
/* 297 */     String trailer = getTrailer();
/* 298 */     if ((trailer != null) && (trailer.length() > 0)) {
/* 299 */       System.err.println();
/* 300 */       System.err.println(trailer);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   protected abstract String getCommandUsage();
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   protected String getTrailer()
/*     */   {
/* 316 */     return "";
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   protected boolean isAuthenticated()
/*     */   {
/* 324 */     return this.mAuth;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   protected String getUser()
/*     */   {
/* 332 */     return this.mUser;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   protected String getServer()
/*     */   {
/* 340 */     return this.mHost;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   protected int getPort()
/*     */   {
/* 348 */     return this.mPort;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   protected SoapTransport getTransport()
/*     */   {
/* 356 */     if (this.mTrans == null)
/* 357 */       initTransport();
/* 358 */     return this.mTrans;
/*     */   }
/*     */   
/*     */   private void initTransport() {
/* 362 */     SoapHttpTransport trans = new SoapHttpTransport(this.mServerUrl);
/* 363 */     trans.setRetryCount(1);
/* 364 */     this.mTrans = trans;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public void setTransportTimeout(int newTimeout)
/*     */   {
/* 372 */     getTransport().setTimeout(newTimeout);
/*     */   }
/*     */   
/*     */   protected String getServerUrl() {
/* 376 */     return this.mServerUrl;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   protected Options getOptions()
/*     */   {
/* 384 */     return this.mOptions;
/*     */   }
/*     */   
/*     */   protected Options getHiddenOptions() {
/* 388 */     return this.mHiddenOptions;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/* 393 */   private static final String[] DATETIME_FORMATS = { "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm:ss SSS", "yyyy/MM/dd HH:mm:ss.SSS", "yyyy/MM/dd-HH:mm:ss-SSS", "yyyy/MM/dd-HH:mm:ss", "yyyyMMdd.HHmmss.SSS", "yyyyMMdd.HHmmss", "yyyyMMddHHmmssSSS", "yyyyMMddHHmmss" };
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 404 */   public static final String CANONICAL_DATETIME_FORMAT = DATETIME_FORMATS[0];
/*     */   
/*     */   public static Date parseDatetime(String str) {
/* 407 */     for (String formatStr : DATETIME_FORMATS) {
/* 408 */       SimpleDateFormat fmt = new SimpleDateFormat(formatStr);
/* 409 */       fmt.setLenient(false);
/* 410 */       ParsePosition pp = new ParsePosition(0);
/* 411 */       Date d = fmt.parse(str, pp);
/* 412 */       if ((d != null) && (pp.getIndex() == str.length()))
/* 413 */         return d;
/*     */     }
/* 415 */     return null;
/*     */   }
/*     */   
/*     */   public static String getAllowedDatetimeFormatsHelp() {
/* 419 */     StringBuilder sb = new StringBuilder();
/* 420 */     sb.append("Specify date/time in one of these formats:\n\n");
/* 421 */     Date d = new Date();
/* 422 */     for (String formatStr : DATETIME_FORMATS) {
/* 423 */       SimpleDateFormat fmt = new SimpleDateFormat(formatStr);
/* 424 */       String s = fmt.format(d);
/* 425 */       sb.append("    ").append(s).append("\n");
/*     */     }
/* 427 */     sb.append("\n");
/*     */     
/* 429 */     sb.append("Specify year, month, date, hour, minute, second, and optionally millisecond.\n");
/*     */     
/* 431 */     sb.append("Month/date/hour/minute/second are 0-padded to 2 digits, millisecond to 3 digits.\n");
/*     */     
/* 433 */     sb.append("Hour must be specified in 24-hour format, and time is in local time zone.\n");
/*     */     
/* 435 */     return sb.toString();
/*     */   }
/*     */   
/*     */   public static ZAuthToken getZAuthToken(CommandLine cl) throws ServiceException, ParseException, IOException {
/* 439 */     if ((cl.hasOption("y")) && (cl.hasOption("Y"))) {
/* 440 */       String msg = String.format("cannot specify both %s and %s options", new Object[] { "y", "Y" });
/*     */       
/* 442 */       throw new ParseException(msg);
/*     */     }
/*     */     
/* 445 */     if (cl.hasOption("y")) {
/* 446 */       return ZAuthToken.fromJSONString(cl.getOptionValue("y"));
/*     */     }
/*     */     
/* 449 */     if (cl.hasOption("Y")) {
/* 450 */       String authToken = StringUtil.readSingleLineFromFile(cl.getOptionValue("Y"));
/* 451 */       return ZAuthToken.fromJSONString(authToken);
/*     */     }
/*     */     
/* 454 */     return null;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/util/SoapCLI.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */